package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.http.res.ResWalletEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.WalletListContract;
import com.taomu.app.zongchouhuli.moudle.mine.entity.WalletListEntity;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class WalletListPresenter implements WalletListContract.Presenter{

    private WalletListContract.View mContractView;

    private CompositeDisposable disposable;

    public WalletListPresenter(WalletListContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getWalletListData(String id,int ship,int take) {
        //setWalletListData(id);
        Request.getIntence().getWallet(StorageHelper.getToken(), id, String.valueOf(ship), String.valueOf(take))
                .flatMap(new Function<String, ObservableSource<BaseChangeResponse<ResWalletEntity>>>() {
                    @Override
                    public ObservableSource<BaseChangeResponse<ResWalletEntity>> apply(String json) throws Exception {
                        BaseResponse<JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResWalletEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResWalletEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseChangeResponse<ResWalletEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResWalletEntity> baseResponse) {
                        if(mContractView != null){
                            mContractView.setWalletListData(baseResponse);
                        }
                    }
                });
    }

    private void setWalletListData(String id){
        List<WalletListEntity> entities = new ArrayList<>();
        if("1".equals(id)){
            for(int i=0;i<5;i++){
                WalletListEntity entity = new WalletListEntity();
                entity.setTitle("产品收益");
                entity.setId(String.valueOf(i));
                entity.setPrice("+100");
                entity.setTime("2018-08-30 14:46");
                entity.setState(id);
                if(i == 1) {
                    entity.setType("审核中");
                }else if(i == 2){
                    entity.setType("已到账");
                }else{
                    entity.setType("");
                }
                entities.add(entity);
            }
            if(mContractView != null){
                //mContractView.setWalletListData(entities);
            }
        }else if("2".equals(id)){
            for(int i=0;i<5;i++){
                WalletListEntity entity = new WalletListEntity();
                entity.setTitle("XX支出");
                entity.setId(String.valueOf(i));
                entity.setPrice("-100");
                entity.setTime("2018-08-30 14:46");
                entity.setType("");
                entity.setState(id);
                entities.add(entity);
            }
            if(mContractView != null){
                //mContractView.setWalletListData(entities);
            }
        }else if("3".equals(id)){
            for(int i=0;i<5;i++){
                WalletListEntity entity = new WalletListEntity();
                entity.setTitle("充值");
                entity.setId(String.valueOf(i));
                entity.setPrice("+100");
                entity.setTime("2018-08-30 14:46");
                entity.setType("");
                entity.setState(id);
                entities.add(entity);
            }
            if(mContractView != null){
                //mContractView.setWalletListData(entities);
            }
        }else if("4".equals(id)) {
            for (int i = 0; i < 5; i++) {
                WalletListEntity entity = new WalletListEntity();
                entity.setTitle("提现");
                entity.setId(String.valueOf(i));
                entity.setPrice("+100");
                entity.setTime("2018-08-30 14:46");
                entity.setType("");
                entity.setState(id);
                entities.add(entity);
            }
            if (mContractView != null) {
                //mContractView.setWalletListData(entities);
            }
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
